package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;

import com.sczmgk.hotline_pzh.DesJson.Des3;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.MyToast;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.view.CustomDeleteInfoView;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改密码的界面
 * Created by Administrator on 2016/12/8 0008.
 */
public class ChangePasswordActivity extends BaseActivity {

    /*旧密码,新密码,确认密码的控件*/
    private CustomDeleteInfoView mOldPwd;
    private CustomDeleteInfoView mNewPwd;
    private CustomDeleteInfoView mConfirmPwd;


    /*提交的按钮*/
    private Button mBtnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);

        //将当前activity添加到集合中
        CommonUtils.addActivity(this);

        setHeader();

        initView();

        initData();

        setListener();
    }

    /**
     * 设置头
     */
    private void setHeader() {
        setTitle(R.string.change_password);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //旧密码,新密码,确认密码的控件
        mOldPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_chang_old_pwd);
        mNewPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_change_new_pwd);
        mConfirmPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_change_password_confirm);

        //提交按钮
        mBtnSubmit = (Button) findViewById(R.id.btn_change_pwd_submit);

        //设置单行
        mOldPwd.setSingleLine();
        mNewPwd.setSingleLine();
        mConfirmPwd.setSingleLine();

        //设置输入类型
        mOldPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        mNewPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        mConfirmPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    /**
     * 初始化数据
     */
    private void initData() {

    }

    /**
     * 设置监听
     */
    private void setListener() {
        mBtnSubmit.setOnClickListener(new MyOnClickListener());
    }

    /**
     * 点击事件
     */
    private class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_change_pwd_submit:
                    //点击提交按钮

                    String oldPwd = mOldPwd.getText();
                    String newPwd = mNewPwd.getText();
                    String confirmPwd = mConfirmPwd.getText();

                    if (verifyPwd(oldPwd, newPwd, confirmPwd)) {
                        //提交
                        submitData(oldPwd, newPwd, confirmPwd);
                    }
                    break;
            }
        }
    }

    private boolean verifyPwd(String oldPwd, String newPwd, String confirmPwd) {
        if (Tools.isEmpty(oldPwd)) {
            MyToast.show(ChangePasswordActivity.this, "请输入密码");
            return false;
        }

        if (Tools.isEmpty(newPwd)) {
            MyToast.show(ChangePasswordActivity.this, "请输入新密码");
            return false;
        }

        if (Tools.isEmpty(confirmPwd)) {
            MyToast.show(ChangePasswordActivity.this, "请再次输入密码");
            return false;
        }

        if (!newPwd.equals(confirmPwd)) {
            MyToast.show(ChangePasswordActivity.this, "两次输入密码不一致");
            return false;
        }
        if (oldPwd.equals(confirmPwd)) {
            MyToast.show(ChangePasswordActivity.this, "新密码与原密码相同请重新输入");
            return false;
        }
        return true;
    }

    /**
     * 提交数据
     *
     * @param oldPwd     老密码
     * @param newPwd     新密码
     * @param confirmPwd 再次确认密码
     */
    private void submitData(String oldPwd, String newPwd, String confirmPwd) {


        Map<String, Object> maps = new HashMap<>();
        maps.put("userid", CacheUtils.getIntData(ChangePasswordActivity.this, Constants.LOGIN_UID, 0));
        maps.put("oldpassword", Des3.getEncodePWD(oldPwd));
        maps.put("newpassword", Des3.getEncodePWD(newPwd));
        MyHttp.requestByCommon(Constants.BASE_URL + Constants.POST_CHAGNE_PWD_URL, maps, MyHttp.POST, ChangePasswordActivity.this, false, null, new SimplePaserJsonCallback() {
            @Override
            public void onVerifyIsNull() {

//                Intent intent = new Intent(ChangePasswordActivity.this, LoginActivity.class);
//                startActivity(intent);
                CacheUtils.cacheBooleanData(ChangePasswordActivity.this , Constants.IS_LOGIN_KEY , false);
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {

            }

            @Override
            public void onErrorCodeError(BaseEntity.MyError mError) {
                super.onErrorCodeError(mError);
                DialogFactory.warningDialog(ChangePasswordActivity.this, mError.msg);
            }
        });


    }

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    public boolean getIsNeedLogin() {
        return true;
    }
}
